package com.attilax.shbye;


import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.jna.Library;
import com.sun.jna.Native;




/**
 
 * 
 */

public class CQZDMDLL
{
	public static String	USERNAME	= "use2015";							//UU用户名
	public static String	PASSWORD	= "000000";							//UU密码
	public static String	DLLPATH		= "lib\\attilaxHelp";					//DLL
	public static String	IMGPATH		= "img\\test.png";
	public static String     img="c:\\test.png";
	public static int		SOFTID		= 95802;								//软件ID
	public static String	SOFTKEY		= "9f87a830ba7b411a98e3a2a378409609";	//软件KEY
	public static String result="";

	
	public interface DM extends Library
	{
		DM	INSTANCE	= (DM) Native.loadLibrary(DLLPATH, DM.class);		
		public int uu_reportError(int id);		
		public int uu_setTimeOut(int nTimeOut);
		public void uu_setSoftInfoA(int softId, String softKey);
		public int uu_loginA(String UserName, String passWord);
		public int uu_getScoreA (String UserName, String passWord);
		public int uu_recognizeByCodeTypeAndBytesA (byte[] picContent, int piclen, int codeType, byte[] returnResult);
		public void uu_getResultA(int nCodeID,String pCodeResult);
 
	
	}
	
	
	public static void main(String[] args) throws Exception
	{
		/*	 DLL 文件MD5值校验
		 *  用处：近期有不法份子采用替换 官方dll文件的方式，极大的破坏了开发者的利益
		 *   
		 *  所以建议所有开发者在软件里面增加校验官方MD5值的函数
		 *  
		 *  checkAPiFile.java是已经写好的校验文件
		 */
				
		int userID;
		
		boolean apiCheckStatus=false;
		apiCheckStatus=checkApiFile.check(DLLPATH+".dll", SOFTID, SOFTKEY);
		
		if(apiCheckStatus){	//判断API文件是否被修改
			DM.INSTANCE.uu_setSoftInfoA(SOFTID, SOFTKEY);	//setsoftinfo和login函数只需要执行一次，就可以无限执行图片识别函数了
			userID=DM.INSTANCE.uu_loginA(USERNAME, PASSWORD);
			if(userID>0){
			//	System.out.println("userID is:"+userID);
			//	System.out.println("user score is:"+DM.INSTANCE.uu_getScoreA(USERNAME, PASSWORD)); 
				
				File f = new File(img);
				byte[] by = toByteArray(f);
				byte[] resultBtye=new byte[30];		//为识别结果申请内存空间
				int codeID=DM.INSTANCE.uu_recognizeByCodeTypeAndBytesA(by, by.length, 8001, resultBtye);	//调用识别函数,resultBtye为识别结果
				String  resultResult = new String(resultBtye,"UTF-8");
				String  resultResult2 = new String(resultBtye,"gbk");
				resultResult=resultResult.trim();
		//System.out.println("this img codeID:"+codeID);
				System.out.println("识别结果:"+resultResult); 
				result=resultResult;
				
			}else{
				System.out.println("er："+userID);	//错误代码请对应 各函数值查看
			} 
		}else{
			System.out.println("对不起，您替换了API文件。 ");
		}
	}

	
	public static byte[] toByteArray(File imageFile) throws Exception
	{
		BufferedImage img = ImageIO.read(imageFile);
		ByteArrayOutputStream buf = new ByteArrayOutputStream((int) imageFile.length());
		try
		{
			ImageIO.write(img, "jpg", buf);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return buf.toByteArray();
	}

	public static byte[] toByteArrayFromFile(String imageFile) throws Exception
	{
		InputStream is = null;

		ByteArrayOutputStream out = new ByteArrayOutputStream();

		try
		{
			is = new FileInputStream(imageFile);
			byte[] b = new byte[1024];
			int n;
			while ((n = is.read(b)) != -1)
			{
				out.write(b, 0, n);
			}// end while

		} catch (Exception e)
		{
			throw new Exception("System error,SendTimingMms.getBytesFromFile", e);
		} finally
		{

			if (is != null)
			{
				try
				{
					is.close();
				} catch (Exception e)
				{}// end try
			}// end if

		}// end try
		return out.toByteArray();
	}

}




